//
//  HomePage.swift
//  DesignCode
//
//  Created by 刘文超 on 2020/5/7.
//  Copyright © 2020 刘文超. All rights reserved.
//

import SwiftUI
let ScreenWidth = UIScreen.main.bounds.size.width
let ScreenHeight = UIScreen.main.bounds.size.height

struct HomePage: View {
    
    @State var showPerson  = false
    @State var ViewSize = CGSize.zero
    var body: some View {
        
        ZStack{
            
            Color.white
                .edgesIgnoringSafeArea(.all)
            
            HomeView(showPerson: $showPerson)
                .padding(.top,44)
                .background(Color.white)
                .clipShape(RoundedRectangle(cornerRadius: 30))
                .shadow(color: Color.black.opacity(0.2), radius: 20, x: 0, y: 20)
                .offset(y: showPerson ? -450 : 0)
                .rotation3DEffect(Angle(degrees: showPerson ? Double(-10) : 0), axis: (x: 10, y: 0, z: 0))
                .scaleEffect(showPerson ? 0.9 : 1)
                .animation(.spring(response: 0.3, dampingFraction: 0.6, blendDuration: 0))
                .edgesIgnoringSafeArea(.all)
            
            MenueView()
                .background(Color.black.opacity(0.001))
                .offset( y: showPerson ? 0 : ScreenHeight)
                .offset( y: ViewSize.height)
                .animation(.spring(response: 0.3, dampingFraction: 0.6, blendDuration: 0))
                .onTapGesture {
                    self.showPerson.toggle()
            }
            .gesture(
                DragGesture().onChanged{value in
                    self.ViewSize = value.translation
                }
                .onEnded{value in
                    
                    if self.ViewSize.height > 50 {
                        self.showPerson = false
                    }
                    self.ViewSize = .zero
                }
                
                
            )
            
        }
        
        
    }
}

struct HomePage_Previews: PreviewProvider {
    static var previews: some View {
        HomePage()
    }
}

//MARK: 设置头像组件
struct AvaterView: View {
    @Binding var showPerson:Bool
    var body: some View {
        Button(action: {
            self.showPerson.toggle()
        }) {
            Image("Avatar")
                .renderingMode(.original)
                .resizable()
                .scaledToFill()
                .frame(width: 36, height: 36)
                .clipShape(Circle())
            
        }
    }
}
